package admin

import (
	"context"
	"fmt"
	"github.com/kataras/iris/v12/mvc"
	"micro-grpc-server/app/common"
	"micro-grpc-server/app/http/controllers"
	pb "micro-grpc-server/services/wechat/proto"
)

type WechatController struct {
	controllers.Controller
	WechatClient pb.WechatService
}

func (c *WechatController) BeforeActivation(b mvc.BeforeActivation) {
	b.Handle("GET", "/tag/{scenario:string}", "TagList")
	b.Handle("GET", "/{scenario:string}/user-list", "UserList")
}

// TagList
// @Router /admin/wechat/tag/{scenario} [get]
// @Summary tags列表
// @Description tags列表
// @Tags Admin - Wechat
// @Produce json
// @Param scenario path string true "场景值" enums(chaping,nicetrip) default(chaping)
// @Success 200 {array} wechat.Tag
func (c *WechatController) TagList(scenario string, ctx context.Context) {
	req := &pb.ScenarioRequest{Scenario: scenario}
	res, err := c.WechatClient.TagsList(ctx, req)
	if err != nil {
		c.BadRequest(fmt.Sprintf("请求失败! -> %s", c.FriendlyRpcError(err)))
		return
	}
	c.JsonSuc(res.List)
}

// UserList
// @Router /admin/wechat/{scenario}/user-list [get]
// @Summary 用户列表
// @Description 用户列表
// @Tags Admin - Wechat
// @Produce json
// @Param scenario path string true "场景值" enums(chaping,nicetrip) default(chaping)
// @Param query query wechat.ListUserRequest false "搜索的数据"
// @Success 200 {array} wechat.User
func (c *WechatController) UserList(scenario string, ctx context.Context) {
	req := &pb.ListUserRequest{Scenario: scenario}
	if err := c.ReadQuery(req); err != nil {
		c.BadRequest(err.Error())
		return
	}
	res, err := c.WechatClient.UserList(ctx, req)
	if err != nil {
		c.BadRequest(fmt.Sprintf("请求失败 -> %s", c.FriendlyRpcError(err)))
		return
	}
	c.JsonSucMeta(res.List, common.Extra{
		Count: res.Total,
		Page:  c.QueryPage(),
	})
}
